package cheqex.update;
import javax.swing.*;

import cheqex.update.controllers.DateController;
import cheqex.update.controllers.RemarksController;
import java.awt.*;
import java.awt.event.*;

public class UpdateDailyView extends JPanel implements ActionListener{

	private JTabbedPane updateDailyReportTabs;
	private JPanel tabPane1 ;
	private JPanel tabPane2;
	private JPanel tabPane3;

	private JTextArea remarks;
    private JPanel remarksPanel;
	private JButton saveRemarks;
	private JLabel remarksLabel;
	
	public UpdateDailyView(){
		initialize();
	}
	
	public void initialize(){
		
		this.updateDailyReportTabs = new JTabbedPane();
		this.tabPane1 = new DepositView();
		this.tabPane2 = new CollectionView();
		this.tabPane3 = new WithdrawalsView();
		
		updateDailyReportTabs.addTab("Deposit",tabPane1);
			updateDailyReportTabs.setMnemonicAt(0, KeyEvent.VK_1);
			
		updateDailyReportTabs.addTab("Collection",tabPane2);
			updateDailyReportTabs.setMnemonicAt(1, KeyEvent.VK_2);
			
		updateDailyReportTabs.addTab("Withdrawals",tabPane3);
			updateDailyReportTabs.setMnemonicAt(2, KeyEvent.VK_3);
		
		updateDailyReportTabs.setVisible(true);
		updateDailyReportTabs.setAutoscrolls(true);
		
		updateDailyReportTabs.setEnabled(true);
		
		//for remarks
		String savedRemarks;
		if(RemarksController.getDailyRemarks(DateController.getDate())!=null){
			savedRemarks = RemarksController.getDailyRemarks(DateController.getDate());
		} else savedRemarks = null;
		
	    saveRemarks = new JButton("Save");
	    saveRemarks.setSize(5,5);
	    saveRemarks.setActionCommand("saveRemarks");
	    saveRemarks.addActionListener(this);
	    remarksLabel = new JLabel("Remarks:");
	    remarks = new JTextArea(savedRemarks,1,60);
	    remarks.setLineWrap(true);
	    remarksPanel = new JPanel();
	    remarksPanel.add(remarksLabel);
	    remarksPanel.add(remarks);
	    remarksPanel.add(saveRemarks);
	    remarksPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		
	    this.add(remarksPanel);
		this.add(updateDailyReportTabs);
		this.setPreferredSize(new Dimension(850,500));
	}
	
	private void addRemarks(){
		try{
			if(RemarksController.getDailyRemarks(DateController.getDate()) == null)
			RemarksController.addDailyRemarks(DateController.getDate(), remarks.getText());
			else RemarksController.updateDailyRemarks(DateController.getDate(), remarks.getText());
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if("saveRemarks".equals(arg0.getActionCommand())){
			addRemarks();
		}
		
	}
}
